#include "BaseEntity.h"

_ImageManager ImageManager;

BaseEntity::BaseEntity()
{
	ClassName = "BaseClass";
}

BaseEntity::~BaseEntity()
{

}

sf::Sprite BaseEntity::GetDrawable()
{
	return Sprite;
}

void BaseEntity::SetModel(std::string filename)
{
	Image = ImageManager.GetImage(filename);
	//Image.LoadFromFile(filename);
	Sprite.SetImage(Image);
	Sprite.SetCenter(Sprite.GetSize().x / 2, Sprite.GetSize().y / 2);

	// Create a box
	b2BodyDef bodyDef;
	bodyDef.position.Set(400.0f, 0.0f);
	b2Body* body = PhysicsWorld->CreateBody(&bodyDef);
	b2PolygonDef shapeDef;
	shapeDef.SetAsBox(Sprite.GetSize().x / 2, Sprite.GetSize().y / 2); // Half the size since the position is counted from the center!
	shapeDef.density = 1.0f; // Default is 0
	shapeDef.friction = 0.2f;
	body->CreateShape(&shapeDef);
	body->SetMassFromShapes();

	PhysicsBody = body;

	//std::cout << ClassName << "'s model was set to \"" << filename << "\"" << std::endl;
}

void BaseEntity::SetPos(float x, float y)
{
	Sprite.SetPosition(x, y);
	
	//std::cout << ClassName << "'s position set to (" << x << "," << y << ")" << std::endl;
}

void BaseEntity::SetAngles(float degrees)
{
	if (degrees >= 360)
	{
		degrees = degrees - 360;
	}

	Sprite.SetRotation(degrees);
}

float BaseEntity::GetAngles()
{
	return Sprite.GetRotation();
}

BaseEntity::pos BaseEntity::GetPos()
{
	pos spritepos;
	spritepos.x = Sprite.GetPosition().x;
	spritepos.y = Sprite.GetPosition().y;

	return spritepos;
}

void BaseEntity::Think()
{
	b2Vec2 position = PhysicsBody->GetPosition();
	float angle = PhysicsBody->GetAngle();
	Sprite.SetPosition(position.x, position.y);
	Sprite.SetRotation(angle * 57.2957795f * -1);
	//std::cout << ClassName << " think" << std::endl;	
}